﻿#include "drv_time.h"
/**********************************************************
函数名：drv_time6_init

函数功能：TIM6初始化

输入参数：无

输出参数：无

其它说明：	定时500MS led状态取反
			预分频=8400-1
			装载值=5000;
			
**********************************************************/

void drv_time6_init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);//开时钟
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//计数模式
	TIM_TimeBaseInitStruct.TIM_Period = 10000;//装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1;//预分频器值
	TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStruct);

	TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);//选择中断源
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM6_DAC_IRQn;//选择通道号
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);

	TIM_Cmd(TIM6, ENABLE);
	
}

void drv_time1_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOE, &GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1);

	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//分频值
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//计数模式
	TIM_TimeBaseInitStruct.TIM_Period = 500;//装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 1-1;//预分频器值
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);

	TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set;//空闲状态电平设置
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//比较输出模式
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;//比较输出极性
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能
	TIM_OCInitStruct.TIM_Pulse = 0;//比较输出脉冲宽度
	TIM_OC1Init(TIM1, &TIM_OCInitStruct);

	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
	TIM_ARRPreloadConfig(TIM1, ENABLE);
	TIM_CtrlPWMOutputs(TIM1, ENABLE);

	TIM_Cmd(TIM1, ENABLE);
}


